package belle.com.myapp.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import java.util.ArrayList;

/**
 * Title :
 * Description :
 * Author:  Belle Guan    date : 2018/5/22 16:04
 * Update :                date :
 * Version : 1.0.0
 * Copyright : copyright(c) 浙江亚厦股份有限公司 2017 ~ 2020 版权所有
 */
public class PagerAdapter extends FragmentStatePagerAdapter {
    private ArrayList<Fragment> fragments;
    private String[] mTitles;
    private int[] mIcons;
    private Context context;

    public PagerAdapter(FragmentManager fm,
                        ArrayList<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    public PagerAdapter(FragmentManager fm,
                        ArrayList<Fragment> fragments, int[] mIcons, Context context) {
        super(fm);
        this.fragments = fragments;
        this.mIcons = mIcons;
        this.context = context;
    }

    public PagerAdapter(FragmentManager fm,
                        ArrayList<Fragment> fragments, String[] mTitles, int[] mIcons, Context context) {
        super(fm);
        this.fragments = fragments;
        this.mIcons = mIcons;
        this.context = context;
        this.mTitles = mTitles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        SpannableString sb;
        Drawable image = context.getResources().getDrawable(mIcons[position]);
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        if (mTitles != null) {
            sb = new SpannableString(" " + mTitles[position]);
        } else {
            sb = new SpannableString(" ");
        }
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }
}
